const { getCurPath } = require('./configUtils');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const devMode = process.env.NODE_ENV !== 'production';

module.exports = {
  entry: getCurPath('../src/main.js'),
  plugins: [
      new CleanWebpackPlugin(),
      new HtmlWebpackPlugin({
        title: "Webpack App",
        template: getCurPath("../index.html")
      }),
      new VueLoaderPlugin(),
      new MiniCssExtractPlugin({
        filename: '[name].[contenthash].css',
        path:getCurPath('../dist')
      })
  ],
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader'
      },
      {
        test: /\.css$/,
        use:['css-loader']
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.(sa|sc|c)ss$/,
        use: [
          devMode ? 'vue-style-loader' : MiniCssExtractPlugin.loader,
          {
            loader: 'css-loader',
            options: {
              esModule: false
            }
          },
          'sass-loader',
        ],
      },
    ]
  },
  output: {
    filename: "[name].[contenthash].js",
    path: getCurPath('../dist'),
    pathinfo: false,
    publicPath: "/",
    assetModuleFilename: "img/[hash][ext][query]"
  },
  optimization: {
    splitChunks: {
      chunks: "all"
    }
  },
  resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      'vue$': 'vue/dist/vue.esm.js',
      '@': getCurPath('../src'),
      'common': getCurPath('../src/common'),
    }
  }
}